package cn.px.rest.controller.upms;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.upms.SysOauthClientDetailsDTO;
import cn.px.dao.entity.upms.SysOauthClientDetails;
import cn.px.dao.vo.query.upms.SysOauthClientDetailsVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.upms.SysOauthClientDetailsService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 终端信息表 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/upms/oauth-client-details")
public class SysOauthClientDetailsController {

    private final SysOauthClientDetailsService sysOauthClientDetailsService;

    /**
     * 通过id查询单条记录
     *
     * @param clientId :
     * @return R :
     */
    @SysLog("查询终端信息")
    @GetMapping("/{clientId}")
    @PreAuthorize("@pms.hasPermission('upms-oauth-client-details-one')")
    public R<SysOauthClientDetails> getById(@PathVariable("clientId") String clientId) {
        return new R<>(sysOauthClientDetailsService.getModelById(clientId));
    }

    /**
     * 新增终端信息
     *
     * @param sysOauthClientDetailsDTO :
     * @return R
     */
    @PostMapping
    @SysLog("新增终端信息")
    @PreAuthorize("@pms.hasPermission('upms-oauth-client-details-add')")
    public R save(@Validated({AddGroup.class}) SysOauthClientDetailsDTO sysOauthClientDetailsDTO) {
        return new R<>(sysOauthClientDetailsService.saveModelByDTO(sysOauthClientDetailsDTO));
    }

    /**
     * 修改终端信息
     *
     * @param sysOauthClientDetailsDTO :
     * @return R
     */
    @PutMapping("/{clientId}")
    @SysLog("修改终端信息")
    @PreAuthorize("@pms.hasPermission('upms-oauth-client-details-edit')")
    public R update(@PathVariable("clientId") String clientId, @Validated({UpdateGroup.class}) SysOauthClientDetailsDTO sysOauthClientDetailsDTO) {
        sysOauthClientDetailsService.modModelByDTO(sysOauthClientDetailsDTO);
        return new R<>();
    }

    /**
     * 删除终端信息
     *
     * @param clientId
     * @return R
     */
    @DeleteMapping("/{clientId}")
    @SysLog("删除终端信息")
    @PreAuthorize("@pms.hasPermission('upms-oauth-client-details-del')")
    public R removeById(@PathVariable String clientId) {
        sysOauthClientDetailsService.deleteByPrimaryKey(clientId);
        return new R<>();
    }

    /**
     * 获取终端信息分页
     *
     * @param pageNum                  当前页
     * @param pageSize                 每页记录数
     * @param sysOauthClientDetailsDTO ${comments}DTO
     * @return R
     */
    @GetMapping("page")
    @SysLog("获取终端信息分页")
    @PreAuthorize("@pms.hasPermission('upms-oauth-client-details-page')")
    public R<AbstractPageResultBean<SysOauthClientDetails>> query(SysOauthClientDetailsDTO sysOauthClientDetailsDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
        return new R<>(sysOauthClientDetailsService.selectPageInfo(sysOauthClientDetailsDTO, pageNum, pageSize));
    }

    /**
     * 获取终端信息列表
     *
     * @param sysOauthClientDetailsDTO :
     * @return :
     */
    @GetMapping("")
    @PreAuthorize("@pms.isClientPermission('end')")
    public R query(SysOauthClientDetailsDTO sysOauthClientDetailsDTO) {
        List<SysOauthClientDetails> modelListByDTO = sysOauthClientDetailsService.getModelListByDTO(sysOauthClientDetailsDTO);
        return new R<>(BeanConverterUtil.copyListProperties(modelListByDTO, SysOauthClientDetailsVO.class));
    }

}
